package com.feige.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.constant.Constants;
import com.feige.dao.DataFileMapper;
import com.feige.pojo.DataFile;
import com.feige.pojo.NotifyMsg;
import com.feige.service.DataFileService;
import com.feige.utils.aliyun.AliyunOssHandler;
import com.feige.result.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * <p>
 * 资料 服务实现类
 * </p>
 *
 * @author feige
 * @since 2021-03-08
 */
@Slf4j
@Service
public class DataFileServiceImpl extends ServiceImpl<DataFileMapper, DataFile> implements DataFileService {


    @Resource
    private RabbitTemplate rabbitTemplate;


    @Override
    public AjaxResult uploadAndSave(MultipartFile file, DataFile dataFile) throws IOException {
        // TODO 班级文件上传推送消息异常未解决
        NotifyMsg notifyMsg = new NotifyMsg();
        notifyMsg
                .setType(2)
                .setCourseId(dataFile.getCourseId());
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        log.info("=====上传文件名：" + originalFilename + "=====");
        int lastIndexOf;
        boolean upload1 = false;
        boolean save = false;
        String fileAddr = AliyunOssHandler.BUCKET_URL;
        String name = "";
        if (originalFilename != null &&
                (lastIndexOf = originalFilename.lastIndexOf(".")) != -1
        ){
            String fileType = originalFilename.substring(lastIndexOf + 1);
            String fileName = IdUtil.simpleUUID();
            if (Arrays.asList(Constants.FILE_TYPE).contains(fileType)){
                name = AliyunOssHandler.joinFileName("file", fileName);
            }
            if (StrUtil.isNotBlank(name)){
                fileAddr += name + "." + fileType;
                log.info("=====开始上传文件=====");
                upload1 = AliyunOssHandler.upload(name + "." + fileType,inputStream);
                log.info("=====文件上传完成=====");
                switch (fileType) {
                    case "doc":
                    case "docx":
                        dataFile.setType(1);
                        break;
                    case "xls":
                    case "xlsx":
                        dataFile.setType(2);
                        break;
                    case "ppt":
                    case "pptx":
                        dataFile.setType(3);
                        break;
                    case "pdf":
                        dataFile.setType(4);
                        break;
                    default:
                        log.info("====格式不正确====");
                        break;
                }
                dataFile.setPath(fileAddr);
                dataFile.setName(originalFilename.substring(0,lastIndexOf));
                save = this.save(dataFile);
                log.info("=====存入数据库完成=====");
            }
        }
        if (upload1 && save){
            notifyMsg
                    .setExtend(dataFile.getPath())
                    .setContent(dataFile.getName());
            rabbitTemplate.convertAndSend("DirectExchange","DirectRouting",notifyMsg);
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}
